package com.unbiz.common.mutable;


import javafx.beans.value.ObservableValueBase;
import javafx.beans.value.WritableValue;
/**
 * extends ObservableValueBase<T>
 * 
 * @version create on 2014年9月22日 下午4:28:44
 */
public class PropertyValue<T>  implements WritableValue<T>{

    protected T value;
    private boolean valid = true;

    public PropertyValue() {
    }

    public PropertyValue(T value) {
        this.value = value;
    }

    /**
     * Returns value.
     */
    public T getValue() {
        return value;
    }

    /**
     * Sets new value.
     */
    public void setValue(T value) {
        this.value = value;
    }

    /**
     * Returns <code>true</code> if value is <code>null</code>.
     */
    public boolean isNull() {
        return value == null;
    }  
    
    
    /**
     * {@inheritDoc}
     */ 
    public T get() {
        valid = true;
       // return observable == null ? value : observable.get();
        return getValue();
    }

    /**
     * {@inheritDoc}
     */  
    public void set(T newValue) {       
        if (value != newValue) {
            value = newValue;
            if (valid) {
                valid = false;
                invalidated();
                //--fireValueChangedEvent();
            }            
        }
    }


    /**
     * The method {@code invalidated()} can be overridden to receive
     * invalidation notifications. This is the preferred option in
     * {@code Objects} defining the property, because it requires less memory.
     *
     * The default implementation is empty.
     */
    protected void invalidated() {
    }
    
    /**
     * Simple to-string representation.
     */
    @Override
    public String toString() {
        if (value == null) {
            return "{" + null + '}';
        }
        return '{' + value.toString() + '}';
    }
}
